package com.example.demoserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demoserver.entity.InteSaleorder;
import com.example.demoserver.service.InteSaleorderService;
import com.example.demoserver.mapper.InteSaleorderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 18364
 * @description 针对表【inte_saleorder】的数据库操作Service实现
 * @createDate 2024-05-31 14:43:29
 */
@Service
public class InteSaleorderServiceImpl extends ServiceImpl<InteSaleorderMapper, InteSaleorder>
        implements InteSaleorderService {
    @Autowired
    InteSaleorderMapper inteSaleorderMapper;

    @Override
    public List<InteSaleorder> selectAll() {
        return inteSaleorderMapper.selectList(null);
    }

    @Override
    public Integer deleteById(Integer id) {
        return inteSaleorderMapper.deleteById(id);
    }

    @Override
    public List<InteSaleorder> selectProduced() {
        QueryWrapper<InteSaleorder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orderState", 3);
        return inteSaleorderMapper.selectList(queryWrapper);
    }

    @Override
    public List<InteSaleorder> selectProduct() {
        QueryWrapper<InteSaleorder> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("orderState", 1, 2, 3);
        return inteSaleorderMapper.selectList(queryWrapper);
    }

    @Override
    public InteSaleorder selectById(Integer id) {

        return inteSaleorderMapper.selectById(id);
    }

    @Override
    public List<InteSaleorder> selectSome(Integer state, Integer day) {
        QueryWrapper<InteSaleorder> queryWrapper = new QueryWrapper<>();
        if (state != null) {
            queryWrapper.eq("orderState", state);
        }
        if (day != null) {
            LocalDateTime startOfToday = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0).withNano(0);
            LocalDateTime endOfToday = LocalDateTime.now().withHour(23).withMinute(59).withSecond(59).withNano(999999999);
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            queryWrapper.between("proEndTime", formatter.format(startOfToday), formatter.format(endOfToday));
        }
        return inteSaleorderMapper.selectList(queryWrapper);
    }

    @Override
    public List<List<InteSaleorder>> sevenDayProduction() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 创建一个List来存储每天的数据
        List<List<InteSaleorder>> ordersByDay = new ArrayList<>();

        for (int i = 1; i <= 7; i++) {
            // 获取当前日期前i天的开始时间和结束时间
            LocalDateTime startOfDay = LocalDateTime.now().minusDays(i).withHour(0).withMinute(0).withSecond(0).withNano(0);
            LocalDateTime endOfDay = LocalDateTime.now().minusDays(i).withHour(23).withMinute(59).withSecond(59).withNano(999999999);

            // 创建QueryWrapper
            QueryWrapper<InteSaleorder> wrapper = new QueryWrapper<>();

            // 使用between方法来查询这一天的数据
            wrapper.between("proEndTime", formatter.format(startOfDay), formatter.format(endOfDay));
            wrapper.eq("orderState",3);
            // 使用你的mapper来执行查询
            List<InteSaleorder> orders = inteSaleorderMapper.selectList(wrapper);

            // 将这一天的数据添加到列表中
            ordersByDay.add(orders);
        }
        return ordersByDay;
    }
}




